<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <AirportTree @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :search-info="searchInfo" :rowSelection="rowSelection">
      <template #toolbar>
        <CollapseContainer class="mt-4">
          <a-button type="primary" @click="handleCreate" :disabled="btnState.disableHandleCreate" v-auth="'ACTION_addViproom'"> 新增贵宾厅 </a-button>
          <a-button type="primary" @click="handleSetAirportExclude" :disabled="btnState.disableSetAirportExclude"> 非共用一键设置 </a-button>
          <a-button type="primary" @click="handleSetViproomServer(selectedRow)" :disabled="!hasSelectedRows">设置服务 </a-button>
          <a-button type="primary" @click="handleSetWeChat(selectedRow)" :disabled="!hasSelectedRows"> 设置上架公众号 </a-button>
          <a-button type="primary" @click="handleLookGoodsLog(selectedRow)" :disabled="!hasSelectedRows"> 查看日志 </a-button>
        </CollapseContainer>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="reload" />
    <ViproomServerSetDrawer @register="registerViproomServerSetDrawer" />
    <GoodsWechatSetDrawer @register="registerGoodsWechatSetDrawer" />
    <GoodsLogModal @register="registerGoodsLogModal"></GoodsLogModal>
    <ExcludeGoodsDrawer @register="registerExcludeGoodsDrawer"></ExcludeGoodsDrawer>
  </PageWrapper>
</template>
<script lang="ts" setup name="airportViproom">
import { reactive } from 'vue';
import { PageWrapper } from '@apps/antd-admin/src/components/page';
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useModal } from '@apps/antd-admin/src/components/modal';
import { CollapseContainer } from '@apps/antd-admin/src/components/Container/index';
import AirportTree from '@apps/ant-bus/src/views/business/airport/components/AirportTree.vue';
import ViproomServerSetDrawer from '../components/ViproomServerSetDrawer.vue';
import ExcludeGoodsDrawer from './ExcludeGoodsDrawer.vue';
import GoodsWechatSetDrawer from '../components/ViproomWechatSetDrawer.vue';
import GoodsLogModal from '../../goodsLog/GoodsLogModal.vue';
import Drawer from './drawer.vue';
import { columns, searchFormSchema } from '../data';
import { Api, setAirportExclude } from '../api';

const { createMessage, createConfirm } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerGoodsLogModal, { openModal: openGoodsLogModal }] = useModal();
const [registerViproomServerSetDrawer, { openDrawer: openViproomServerSetDrawer }] = useDrawer();
const [registerGoodsWechatSetDrawer, { openDrawer: openGoodsWechatSetDrawer }] = useDrawer();
const [registerExcludeGoodsDrawer, { openDrawer: openExcludeGoodsDrawer }] = useDrawer();
const searchInfo = reactive<Recordable>({ viproomType: 1 });
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'vipromm1-template',
  tableProps: {
    //表格标题
    title: '机场贵宾厅列表',
    //主键字段
    rowKey: 'viproomId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showExportButton: true,
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
    //操作列配置
    actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows, selectedRow }] = tableContext;
const btnState = reactive({
  disableHandleCreate: true,
  disableSetAirportExclude: true,
});

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
    airportId: searchInfo.airportId,
  });
}

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'ACTION_editViproom',
    },
    {
      label: '设置非共用',
      onClick: handleSetExclude.bind(null, record),
    },
  ];
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
  reload();
}

function handleSetExclude(record: Recordable) {
  openExcludeGoodsDrawer(true, {
    record,
    isUpdate: true,
  });
}
function handleSetAirportExclude() {
  createConfirm({
    iconType: 'info',
    title: '一键设置不可共用贵宾厅',
    content: '确认一键设置不可共用贵宾厅？',
    onOk: async () => {
      await setAirportExclude({ airportId: searchInfo.airportId });
    },
    onCancel: async () => {},
  });
}

function handleSelect(airportId) {
  btnState.disableHandleCreate = false;
  btnState.disableSetAirportExclude = false;
  searchInfo.airportId = airportId;
  console.info('searchInfo', searchInfo);
  reload();
}

function handleSetViproomServer(record: Recordable) {
  openViproomServerSetDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleSetWeChat(record: Recordable) {
  openGoodsWechatSetDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleLookGoodsLog(record: Recordable) {
  let data = { goodsId: record.viproomId };
  openGoodsLogModal(true, {
    data,
    isUpdate: true,
  });
}
</script>
